<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include('menu.php'); ?>
</head>
<body>
<?php
//ลบข้อมูล
if($_GET["action"]=='del') {

	$Query = "DELETE FROM category WHERE category_id = '".$_GET["id"]."'";
	$objQuery = mysql_query($Query);
	if(!$objQuery)
	{
		echo "Error Delete [".mysql_error()."]";
	}
	
	header('Location: category_manage.php');
}

//ค้นหาสินค้า
if($_POST["code_search"] != "" OR $_POST["name_search"] != "" OR $_POST["category_search"] != "" OR $_POST["cost_search"] != "" OR $_POST["price_search"] != "") {
	
		$Query = "SELECT * FROM product,producttype WHERE product.prodtype_id = producttype.prodtype_id ";
	
	if($_POST["code_search"] != "") {
		$Query .= "AND prod_barcode LIKE '%".$_POST["code_search"]."%' ";
	}
	if($_POST["name_search"] != "") {
		$Query .= "AND prod_name LIKE '%".$_POST["name_search"]."%' ";
	}
	if($_POST["category_search"] != "") {
		$Query .= "AND prodtype_name LIKE '%".$_POST["category_search"]."%' ";
	}
	if($_POST["price_search"] != "") {
		$Query .= "AND prod_price LIKE '%".$_POST["price_search"]."%' ";
	}
	$Query .= "ORDER BY prod_name";
	
} else {
	$Query = "SELECT * FROM category ORDER BY category_name";
}
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>จัดการหมวดหมู่</h1>
    <div class="buttons"><a onclick="JavaScript:window.location='category_add.php'" class="button"><span>เพิ่มหมวดหมู่</span></a></div></div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
            <td width="70" style="text-align: center;"><span class="center">ลำดับ</span></td>
            <td class="center">ชื่อหมวดหมู่</td>
            <td  width="350"  class="center">จัดการหมวดหมู่</td>
          </tr>
        </thead>
        <tbody>
<?php
	$num=0;
	while($row = mysql_fetch_array($objQuery))
		{
			$num++;
?>
          <tr>
            <td style="text-align: center;"><?php echo $num;?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["category_name"];?></td>
			<td class="center">[ <a href="category_edit.php?id=<?php echo $row["category_id"];?>">แก้ไข</a> ] [ <a href="JavaScript:if(confirm('คุณต้องการยืนยันการลบข้อมูล ใช่หรือไม่?')==true){window.location='<?php echo $_SERVER["PHP_SELF"];?>?action=del&id=<?php echo $row["category_id"];?>';}">ลบ</a> ]</td>
          </tr>
<?php
		} 
?>
        </tbody>
      </table>
  </div>
</div>

</body>
<?php mysql_close(); ?>
</html>
